<template>
	<view class="template-atlas">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back'>
				<text class='icon tn-icon-left' @click="goBack"></text>
				<text class='icon tn-icon-home-capsule-fill' @click="goHome"></text>
			</view>
		</tn-nav-bar>

		<view class="tn-color-white">
			<view class="icon15__item--icon tn-flex tn-flex-row-center tn-flex-col-center tn-shadow-blur button-3"
				@tap="showLandscape">
				<view class="tn-icon-level"></view>
			</view>
			<view class="icon15__item--icon tn-flex tn-flex-row-center tn-flex-col-center tn-shadow-blur button-2"
				@click="tn('/pageA/author/author?id='+productData.author.userId)">
				<view class="tn-icon-my"></view>
			</view>
			<!-- <view class="icon15__item--icon tn-flex tn-flex-row-center tn-flex-col-center tn-shadow-blur button-1"
				@click="tn('/pageB/review/review')">
				<view class="tn-icon-comment"></view>
			</view> -->
		</view>

		<!-- 页面内容 -->
		<!-- <view class=""> -->
		<scroll-view scroll-y="true" style="height: 100%;">

			<span class="ql-editor" v-html="productData.richText" @click="richTextClick" />

		</scroll-view>
		<!-- <view class="slideshow-image" :style="`background-image: url(${productData.allImg})`">
			</view> -->
		<!-- <view class="slideshow-image"
				style="background-image: url('https://cdn.nlark.com/yuque/0/2022/jpeg/280373/1664005699053-assets/web-upload/8645ea3a-e0a9-4422-8364-cc5ede305c9f.jpeg')">
			</view>
			<view class="slideshow-image"
				style="background-image: url('https://cdn.nlark.com/yuque/0/2022/jpeg/280373/1664179989916-assets/web-upload/eda197eb-42ce-44b1-9b14-fce3481db603.jpeg')">
			</view>
			<view class="slideshow-image"
				style="background-image: url('https://cdn.nlark.com/yuque/0/2022/jpeg/280373/1664179989980-assets/web-upload/ff5a1f9d-c029-43cc-9a40-1f1ce72b664d.jpeg')">
			</view> -->
		<!-- </view> -->

		<!-- 底部tabbar start-->
		<view class="tabbar footerfixed dd-glass tn-color-white" style="border-radius: 100rpx;">

			<view class="action">
				<view class="bar-icon">
					<view class="tn-icon-footprint">
						<tn-badge v-if="productData.viewCount>0" :absolute='true' :translateCenter="true" :fontSize='24'
							backgroundColor='rgba(0, 0, 0, 0)'>{{productData.viewCount}}</tn-badge>
					</view>

				</view>
				<view class="">浏览</view>
			</view>

			<view class="action" @click="clickLike(!productData.like)">
				<view class="bar-icon">
					<view :class="productData.like?'tn-icon-like-fill':'tn-icon-like-lack'">
						<tn-badge v-if="productData.likeCount>0" :absolute='true' :translateCenter="true" :fontSize='24'
							backgroundColor='rgba(0, 0, 0, 0)'>{{productData.likeCount}}</tn-badge>
					</view>
				</view>
				<view class="">收藏</view>
			</view>

			<view :class="banDownload?'action tn-color-gray':'action'" @click="clickDownload">
				<view class="bar-icon">
					<view class="tn-icon-download">
						<tn-badge v-if="productData.downloadCount>0" :absolute='true' :translateCenter="true"
							:fontSize='24' backgroundColor='rgba(0, 0, 0, 0)'>{{productData.downloadCount}}</tn-badge>
					</view>
				</view>
				<view class="">下载</view>
			</view>

			<!-- <view class="action">
        <view class="bar-icon">
          <view class="tn-icon-flower">
          </view>
          <image class="" src='https://tnuiimage.tnkjapp.com/tabbar/i2.png'></image> 
        </view>
        <view class="">海报</view>
      </view> -->

			<view class="action">
				<button class="tn-flex-direction-column tn-flex-row-center tn-flex-col-center tn-button--clear-style"
					open-type="share">
					<view class="bar-icon">
						<view class="tn-icon-send">
						</view>
						<!-- <image class="" src='https://tnuiimage.tnkjapp.com/tabbar/d2.png'></image> -->
					</view>
					<view class="">分享</view>
				</button>
			</view>
		</view>

		<!-- 压屏窗-->
		<tn-landscape :show="show1" @close="closeLandscape" closePosition="bottom" :closeSize="60">
			<view class="tn-color-white" style="width: 100vw;">

				<view class="" style="margin: 120rpx 60rpx;">
					<view class="tn-flex tn-flex-col-top">

						<view @click="tn('/pageA/author/author?id='+productData.author.userId)">
							<view class="logo-pic tn-shadow">
								<view class="logo-image">
									<view class="tn-shadow-blur"
										:style="'background-image:url('+productData.author.headImg+');width: 100rpx;height: 100rpx;background-size: cover;'">
									</view>
								</view>
							</view>
							<view :class="sex.icon"
								:style="'color:'+sex.color+' ;position: absolute;margin: -105rpx 0 0 72rpx;border-radius: 100rpx;background-color: #FFFFFF;padding: 5rpx;'">
							</view>
						</view>

						<view class="tn-padding-left-sm tn-padding-top-xs" style="width: 100%;">
							<view class="tn-flex tn-flex-row-between tn-flex-col-between">
								<view class="justify-content-item">
									<text class="tn-text-lg tn-text-bold">{{productData.author.username}}</text>
									<span v-if="constellation">
										<text
											class="tn-padding-left-sm tn-padding-right-xs">{{constellation.text}}</text>
										<text v-if="constellation" :class="constellation.icon"></text>
									</span>
								</view>
								<!-- <view class="justify-content-item tn-round tn-text-xs tn-bg-orangered--light tn-color-orangered" style="padding: 5rpx 15rpx;">
                  <text class="tn-icon-warning-fill tn-padding-right-xs"></text> 举报
                </view> -->
							</view>
							<view class="tn-padding-top-xs">
								<view class="">
									<text class="tn-text-df tn-color-gray--light">{{productData.createDate}}</text>
								</view>
							</view>
						</view>
					</view>


					<view v-if="productData.missFont" class="tn-margin-top-sm">
						<text user-select class="tn-text-bold"> 相关字体：{{productData.missFont}}</text>
					</view>
					<view class="tn-flex tn-flex-row-between tn-flex-col-between tn-margin-top-sm tn-text-justify">
						<scroll-view scroll-y="true" style="height: 30vh;">
							<text user-select> {{productData.shortDesc}} </text>
						</scroll-view>
					</view>
				</view>


			</view>
		</tn-landscape>

	</view>
</template>

<script>
	import {
		isCollection,
		collectionHandler
	} from '@/utils/userActionUtil.js';

	import {
		likeProductById,
		getProductInfo,
		getProductDownloadUrl
	} from '@/api/product.js';

	import {
		getDictLabel
	} from '@/utils/dictUtil.js';

	import {
		loginVerify
	} from '@/utils/loginUtil.js';

	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	export default {
		name: 'TemplateAtlas',
		mixins: [template_page_mixin],
		data() {
			return {
				banDownload: false, //禁止下载
				sex: {}, //性别
				constellation: {}, //星座
				id: 0,
				productData: {
					author: {}
				},
				show1: false,
				networkError: true,
			}
		},
		onLoad(options) {
			this.id = options.id
			// this.getInitData()
		},
		onShow() {
			// 重新进来更新一下，防止触发黑名单机制，同时更新详情
			this.getInitData()
		},
		methods: {
			// 点击下载
			clickDownload() {
				if (this.banDownload) {
					return
				}

				if (this.networkError) {
					uni.showToast({
						icon: 'none',
						title: '网络异常，请重新进入页面'
					})
					return
				}

				if (!loginVerify()) {
					return
				}

				uni.showModal({
					title: `消耗${this.productData.salesPrice}积分`,
					content: '15分钟内重复下载不会扣减积分',
					cancelText: '取消',
					confirmText: '下载',
					success: showRes => {
						if (showRes.confirm) {
							this.banDownload = true
							getProductDownloadUrl(this.id).then(res => {
								this.productData.downLoadCount++
							}).finally(() => {
								setTimeout(() => {
									this.banDownload = false
								}, 3000)
							})
						}
					}
				})

			},
			// 点击收藏
			clickLike(status) {
				if (!loginVerify()) {
					return
				}
				likeProductById(this.id, status ? 1 : 0).then(res => {
					this.productData.like = status
					if (this.productData.like) {
						this.productData.likeCount++
					} else {
						this.productData.likeCount--
					}
				})
			},
			// 富文本点击事件
			richTextClick() {
				uni.previewImage({
					current: this.richImgList[0], // 以后显示图片的http链接
					urls: this.richImgList
				})
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			// 弹出压屏窗
			showLandscape() {
				this.openLandscape()
			},
			// 打开压屏窗
			openLandscape() {
				this.show1 = true
			},
			// 关闭压屏窗
			closeLandscape() {
				this.show1 = false
			},
			getInitData() {
				getProductInfo(this.id).then((res) => {
					if (!res.data) {
						return
					}
					this.networkError = false
					this.productData = res.data;

					// 处理富文本
					if (this.productData.richText.indexOf("src") >= 0) {
						this.productData.richText = this.productData.richText.replace(/\<img/gi,
							'<img style=max-width:100%;height:auto');
						const imgs = [];
						this.productData.richText.replace(/]*src=['"]([^'"]+)[^>]*>/gi, function(match, capture) {
							imgs.push(capture)
						})
						this.richImgList = imgs
					}

					// 处理收藏状态
					this.isCollection = isCollection(this.id)

					// 处理性别
					const author = this.productData.author
					this.sex = getDictLabel("gender", author.sex)
					if (author.constellation) {
						this.constellation = getDictLabel("constellation", author.constellation)
					}
				}).catch(() => {
					this.networkError = true
				});

			},
			// 定义全局分享
			// 1.发送给朋友
			onShareAppMessage(res) {
				let path = 'pageA/atlas/atlas?id=' + this.id
				if (this.vuex_user) {
					path = path + '&fromUserId=' + this.vuex_user.userId
				}
				return {
					title: this.productData.title,
					path: path,
					imageUrl: this.productData.icon,
				}
			},
			//2.分享到朋友圈
			onShareTimeline(res) {
				let query = 'id=' + this.id
				if (this.vuex_user) {
					query = query + '&fromUserId=' + this.vuex_user.userId
				}
				return {
					title: this.productData.title,
					query: query,
					imageUrl: this.productData.icon,
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import url("@/tuniao-ui/quill.core.css");

	.template-atlas {
		margin: 0;
		width: 100%;
		height: 100vh;
		overflow: hidden;
	}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 图标容器15 start */
	.icon15 {
		&__item {
			width: 30%;

			border-radius: 10rpx;
			padding: 30rpx;
			margin: 20rpx 10rpx;
			transform: scale(1);
			transition: transform 0.3s linear;
			transform-origin: center center;

			&--icon {
				width: 100rpx;
				height: 100rpx;
				font-size: 50rpx;
				border-radius: 50%;
				margin-bottom: 18rpx;
				position: relative;
				z-index: 1;

				&::after {
					content: " ";
					position: absolute;
					z-index: -1;
					width: 100%;
					height: 100%;
					left: 0;
					bottom: 0;
					border-radius: inherit;
					opacity: 1;
					transform: scale(1, 1);
					background-size: 100% 100%;


				}
			}
		}
	}

	/* 按钮 */
	.button-1 {
		background-color: rgba(0, 0, 0, 0.15);
		position: fixed;
		/* bottom:200rpx;
      right: 20rpx; */
		top: 25%;
		right: 30rpx;
		z-index: 1001;
		border-radius: 100px;
	}

	.button-2 {
		background-color: rgba(0, 0, 0, 0.15);
		position: fixed;
		/* bottom:200rpx;
      right: 20rpx; */
		top: 35%;
		right: 30rpx;
		z-index: 1001;
		border-radius: 100px;
	}

	.button-3 {
		background-color: rgba(0, 0, 0, 0.15);
		position: fixed;
		/* bottom:200rpx;
      right: 20rpx; */
		top: 45%;
		right: 30rpx;
		z-index: 1001;
		border-radius: 100px;
	}



	/* 相册 start*/
	.slideshow {
		top: 0;
		position: absolute;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
	}

	.slideshow-image {
		position: absolute;
		width: 100%;
		height: 100%;
		background: no-repeat 50% 50%;
		background-size: cover;
		-webkit-animation-name: kenburns;
		animation-name: kenburns;
		-webkit-animation-timing-function: linear;
		animation-timing-function: linear;
		-webkit-animation-iteration-count: infinite;
		animation-iteration-count: infinite;
		-webkit-animation-duration: 16s;
		animation-duration: 16s;
		opacity: 1;
		transform: scale(1.2);
	}

	// .slideshow-image:nth-child(1) {
	// 	-webkit-animation-name: kenburns-1;
	// 	animation-name: kenburns-1;
	// 	z-index: 3;
	// }

	// .slideshow-image:nth-child(2) {
	// 	-webkit-animation-name: kenburns-2;
	// 	animation-name: kenburns-2;
	// 	z-index: 2;
	// }

	// .slideshow-image:nth-child(3) {
	// 	-webkit-animation-name: kenburns-3;
	// 	animation-name: kenburns-3;
	// 	z-index: 1;
	// }

	// .slideshow-image:nth-child(4) {
	// 	-webkit-animation-name: kenburns-4;
	// 	animation-name: kenburns-4;
	// 	z-index: 0;
	// }

	@-webkit-keyframes kenburns-1 {
		0% {
			opacity: 1;
			transform: scale(1.2);
		}

		1.5625% {
			opacity: 1;
		}

		23.4375% {
			opacity: 1;
		}

		26.5625% {
			opacity: 0;
			transform: scale(1);
		}

		100% {
			opacity: 0;
			transform: scale(1.2);
		}

		98.4375% {
			opacity: 0;
			transform: scale(1.2117647059);
		}

		100% {
			opacity: 1;
		}
	}

	@keyframes kenburns-1 {
		0% {
			opacity: 1;
			transform: scale(1.2);
		}

		1.5625% {
			opacity: 1;
		}

		23.4375% {
			opacity: 1;
		}

		26.5625% {
			opacity: 0;
			transform: scale(1);
		}

		100% {
			opacity: 0;
			transform: scale(1.2);
		}

		98.4375% {
			opacity: 0;
			transform: scale(1.2117647059);
		}

		100% {
			opacity: 1;
		}
	}

	@-webkit-keyframes kenburns-2 {
		23.4375% {
			opacity: 1;
			transform: scale(1.2);
		}

		26.5625% {
			opacity: 1;
		}

		48.4375% {
			opacity: 1;
		}

		51.5625% {
			opacity: 0;
			transform: scale(1);
		}

		100% {
			opacity: 0;
			transform: scale(1.2);
		}
	}

	@keyframes kenburns-2 {
		23.4375% {
			opacity: 1;
			transform: scale(1.2);
		}

		26.5625% {
			opacity: 1;
		}

		48.4375% {
			opacity: 1;
		}

		51.5625% {
			opacity: 0;
			transform: scale(1);
		}

		100% {
			opacity: 0;
			transform: scale(1.2);
		}
	}

	@-webkit-keyframes kenburns-3 {
		48.4375% {
			opacity: 1;
			transform: scale(1.2);
		}

		51.5625% {
			opacity: 1;
		}

		73.4375% {
			opacity: 1;
		}

		76.5625% {
			opacity: 0;
			transform: scale(1);
		}

		100% {
			opacity: 0;
			transform: scale(1.2);
		}
	}

	@keyframes kenburns-3 {
		48.4375% {
			opacity: 1;
			transform: scale(1.2);
		}

		51.5625% {
			opacity: 1;
		}

		73.4375% {
			opacity: 1;
		}

		76.5625% {
			opacity: 0;
			transform: scale(1);
		}

		100% {
			opacity: 0;
			transform: scale(1.2);
		}
	}

	@-webkit-keyframes kenburns-4 {
		73.4375% {
			opacity: 1;
			transform: scale(1.2);
		}

		76.5625% {
			opacity: 1;
		}

		98.4375% {
			opacity: 1;
		}

		100% {
			opacity: 0;
			transform: scale(1);
		}
	}

	@keyframes kenburns-4 {
		73.4375% {
			opacity: 1;
			transform: scale(1.2);
		}

		76.5625% {
			opacity: 1;
		}

		98.4375% {
			opacity: 1;
		}

		100% {
			opacity: 0;
			transform: scale(1);
		}
	}

	/* 相册 end*/

	/* 用户头像 start */
	.logo-image {
		width: 100rpx;
		height: 100rpx;
		position: relative;
	}

	.logo-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border: 6rpx solid rgba(255, 255, 255, 0.25);
		box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
		border-radius: 50%;
		overflow: hidden;
		// background-color: #FFFFFF;
	}

	/* 底部tabbar start*/
	/* 毛玻璃*/
	.dd-glass {
		width: 100%;
		backdrop-filter: blur(20rpx);
		-webkit-backdrop-filter: blur(20rpx);
	}

	.footerfixed {
		position: fixed;
		// margin: 20rpx;
		margin: 40rpx 5%;
		width: 90%;
		bottom: calc(env(safe-area-inset-bottom) / 2);
		;
		z-index: 999;
		background-color: rgba(0, 0, 0, 0.15);
		box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
	}

	.tabbar {
		display: flex;
		align-items: center;
		min-height: 110rpx;
		justify-content: space-between;
		padding: 0;
		height: calc(110rpx + env(safe-area-inset-bottom) / 2);
		// padding-bottom: calc(env(safe-area-inset-bottom) / 2);
	}

	.tabbar .action {
		font-size: 22rpx;
		position: relative;
		flex: 1;
		text-align: center;
		padding: 0;
		display: block;
		height: auto;
		line-height: 1;
		margin: 0;
		overflow: initial;
	}

	.tabbar .action .bar-icon {
		width: 100rpx;
		position: relative;
		display: block;
		height: auto;
		margin: 0 auto 10rpx;
		text-align: center;
		font-size: 42rpx;
	}

	.tabbar .action .bar-icon image {
		width: 50rpx;
		height: 50rpx;
		display: inline-block;
	}
</style>